package me.like.diandian.util.updata;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;

import io.reactivex.functions.Consumer;
import me.like.diandian.R;
import me.like.diandian.databinding.DialogGengxinBinding;

import static com.jakewharton.rxbinding2.view.RxView.clicks;


/**
 * Created by Administrator on 2016/11/22.
 */

public class UpdataDialog extends Dialog {

    private DialogGengxinBinding binding;
    private String title;
    private String content;
    private boolean isQiangzhi = false;


    public UpdataDialog(Context context, String content, boolean isQiangzhi) {
        super(context, R.style.MyDialogStyleTop);
        this.content = content;
        this.isQiangzhi = isQiangzhi;
    }

    public UpdataDialog(Context context, String content, String title, boolean isQiangzhi) {
        super(context, R.style.MyDialogStyleTop);
        this.content = content;
        this.isQiangzhi = isQiangzhi;
        this.title = title;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View inflate = View.inflate(getContext(), R.layout.dialog_gengxin, null);
        binding = DataBindingUtil.bind(inflate);
        setContentView(binding.getRoot());
        setCanceledOnTouchOutside(false);
        if (!TextUtils.isEmpty(content)) {
            binding.tvContent.setText(content);
        }
        if (!TextUtils.isEmpty(title)) {
            binding.tvTitle.setText(title);
        }
        setCancelable(isQiangzhi);

        if (isQiangzhi) {
            binding.lyTwo.setVisibility(View.GONE);
            binding.lyOne.setVisibility(View.VISIBLE);
        } else {
            binding.lyTwo.setVisibility(View.VISIBLE);
            binding.lyOne.setVisibility(View.GONE);
        }
        clicks(binding.tvQuxiao).subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                dismiss();
            }
        });
        clicks(binding.tvQueding).subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
               onOkClick();
            }
        });
        clicks(binding.tvQueding1).subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                onOkClick();
            }
        });
    }


    public void onOkClick() {
        dismiss();
    }


    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
